//
//  joinPhotoViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/8/31.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "joinPhotoViewController.h"
#import "postPhotoViewController.h"
#import "AFHTTPSessionManager.h"
#import "UploadImage.h"
#import "Macros.h"
#import "TCLYUser.h"
#import "HttpRequest.h"

@interface joinPhotoViewController ()<UITextFieldDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UIButton *imageButton01;
@property (weak, nonatomic) IBOutlet UIButton *imageButton02;
@property (weak, nonatomic) IBOutlet UIButton *imageButton03;
//设置图片
@property(nonatomic,strong)UIImage *image01;
@property(nonatomic,strong)UIImage *image02;
@property(nonatomic,strong)UIImage *image03;

@property(nonatomic,strong)NSMutableArray *picArray;
@property(nonatomic,assign)int num;
@property(nonatomic,copy)NSString*picString;
@property(nonatomic,assign)int isTrue;
@end

@implementation joinPhotoViewController
@synthesize picArray,num,image01,image02,image03;
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.isTrue=0;
    self.view.backgroundColor=[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0];
    self.headerView.backgroundColor=[UIColor colorWithRed:243/255.0 green:143/255.0 blue:177/255.0 alpha:1.0];
    self.imageButton01.tag=1;
    self.imageButton02.tag=2;
    self.imageButton03.tag=3;
    picArray=[NSMutableArray array];
}


- (IBAction)goBack:(id)sender
{
    postPhotoViewController *ppvc=self.navigationController.viewControllers[1];
    [self.navigationController popToViewController:ppvc animated:YES];
    
}
- (IBAction)onClick:(id)sender
{
    UIButton *btn=sender;
    if (btn.tag==1)
    {
        num=1;
    }
    else if (btn.tag==2)
    {
        num=2;
    }
    else if (btn.tag==3)
    {
        num=3;
    }
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"从手机相册选择" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                             {
                                 [self addPhotoImage];
                             }];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];

}
-(void)addPhotoImage
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:nil];
}
#pragma mark -- <UIImagePickerControllerDelegate>--
// 获取图片后的操作
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    // 销毁控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
    if (self.num==1)
    {
        image01 = info[UIImagePickerControllerOriginalImage];
        [self.imageButton01 setImage:image01 forState:UIControlStateNormal];
        self.isTrue=1;
        
    }
    else if (self.num==2)
    {
        image02 = info[UIImagePickerControllerOriginalImage];
        [self.imageButton02 setImage:image02 forState:UIControlStateNormal];
        self.isTrue=1;

    }
    else if (self.num==3)
    {
        image03 = info[UIImagePickerControllerOriginalImage];
        [self.imageButton03 setImage:image03 forState:UIControlStateNormal];
        self.isTrue=1;
    }
}

- (IBAction)faBuZhaoPian:(id)sender
{
    if (self.isTrue==1)
    {
        if (self.num==1)
        {
            [picArray addObject:image01];
        }
        else if (self.num==2)
        {
            [picArray addObject:image01];
            [picArray addObject:image02];
        }
        else if (self.num==3)
        {
            [picArray addObject:image01];
            [picArray addObject:image02];
            [picArray addObject:image03];
        }
        [self sendDataFromNet01];
        if (picArray.count>0)
        {
            for (UIImage *image in picArray)
            {
                self.picString=[self UIImageToBase64Str:image];
                [self updateDataFromNet:self.picString];
            }
        }
    }
    else{
        UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"请选择要发布的照片" message:nil preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                                 {
                                     nil;
                                 }];
        [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];
    
    }
}
//图片转字符串
-(NSString *)UIImageToBase64Str:(UIImage *) image
{
    NSData *data = UIImageJPEGRepresentation(image, 0.1f);
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return encodedImageStr;
}
//网服务器发布标题
-(void)sendDataFromNet01
{
    NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/active/request_data"];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"user_id"]=userID;
    parameters[@"content"]=self.textView.text;
    NSLog(@"userID=%@",userID);
    NSLog(@"self.textView.text=%@",self.textView.text);
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         int msg=[[json objectForKey:@"msg"]intValue];
         if (msg==200)
         {
             NSLog(@"发布帖子成功");
         }
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
-(void)updateDataFromNet:(NSString*)imageString
{
    NSString *urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/active/request_data_pic"];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"user_id"]=userID;
    parameters[@"image"]=imageString;
    parameters[@"content"]=self.textView.text;
    //    NSLog(@"imageString=%@",imageString);
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         //获取数组
         NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
